﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LoRel;

namespace ExampleImplementation
{
    public class SymbolAndTraderKey : Key
    {
        private readonly string m_Symbol;
        private readonly String m_Trader;

        public SymbolAndTraderKey(string symbol, string trader)
        {
            m_Symbol = symbol;
            m_Trader = trader;
        }

        public string Symbol
        {
            get { return m_Symbol; }
        }

        public string Trader
        {
            get { return m_Trader; }
        }
        
        #region equality and compare 

        public bool Equals(SymbolAndTraderKey other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Equals(other.m_Symbol, m_Symbol) && Equals(other.m_Trader, m_Trader);
        }

        public bool Equals(Key other)
        {
            SymbolAndTraderKey rhs = other as SymbolAndTraderKey;
            return Equals(rhs);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof (SymbolAndTraderKey)) return false;
            return Equals((SymbolAndTraderKey) obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return ((m_Symbol != null ? m_Symbol.GetHashCode() : 0)*397) ^ (m_Trader != null ? m_Trader.GetHashCode() : 0);
            }
        }

        public int CompareTo(object obj)
        {
            SymbolAndTraderKey rhs = obj as SymbolAndTraderKey;
            if (rhs == null) return m_Symbol.CompareTo(obj);

            int compare = m_Symbol.CompareTo(rhs.m_Symbol);
            if (compare == 0) compare = m_Trader.CompareTo(rhs.m_Trader);
            return compare;
        }

        #endregion
    }

    public class SymbolKey : Key
    {
        private readonly String m_Symbol;

        public string Symbol
        {
            get { return m_Symbol; }
        }

        public SymbolKey(string symbol)
        {
            m_Symbol = symbol;
        }

        #region compare and equality

        public bool Equals(SymbolKey other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Equals(other.m_Symbol, m_Symbol);
        }

        public bool Equals(Key other)
        {
            SymbolKey rhs = other as SymbolKey;
            if (rhs != null) Equals(rhs);
            return false;
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof (SymbolKey)) return false;
            return Equals((SymbolKey) obj);
        }

        public override int GetHashCode()
        {
            return (m_Symbol != null ? m_Symbol.GetHashCode() : 0);
        }

        public int CompareTo(object obj)
        {
            if (obj is SymbolKey) return m_Symbol.CompareTo(( (SymbolKey) obj).m_Symbol);

            return m_Symbol.CompareTo(obj);
        }

        #endregion
    }
}
